/**   
 * Copyright © 2021 郑心怡. All rights reserved.
 * 
 * @Title: UserInfoController.java 
 * @Prject: com.interesting.examsystem.user.controller
 * @Package: com.interesting.examsystem.user.controller
 * @author: ZhengXinyi   
 * @date: 2021-07-24
 * @version: V1.0   
 */
package com.interesting.examsystem.user.controller;

import java.util.Arrays;
import java.util.List;

import com.interesting.examsystem.common.exception.DuplicateUserNameException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.interesting.examsystem.common.annotation.LogAnnotation;
import com.interesting.examsystem.common.constant.RestConstant;
import com.interesting.examsystem.common.core.controller.BaseController;
import com.interesting.examsystem.common.enums.OperateTypeEnum;
import com.interesting.examsystem.common.response.Result;
import com.interesting.examsystem.common.response.ResultGenerator;
import com.interesting.examsystem.user.model.UserInfoDTO;
import com.interesting.examsystem.user.model.UserInfoVO;
import com.interesting.examsystem.user.service.IUserInfoService;
import com.interesting.examsystem.common.utils.InputInjectFilter;

/**
 * @ClassName: UserInfoController
 * @Description:  控制层
 * @author: ZhengXinyi 
 * @date: 2021-07-24
 */
@RestController
@RequestMapping(UserInfoController.PATH)
@Api(tags = "系统->用户")
public class UserInfoController extends BaseController {
	public final static String PATH = RestConstant.VERSION_V1 + "/sys/user/info";
	// 日志审计列表中“菜单路径”
	private static final String MENU_NAME = "系统->用户";
	// 日志审计列表中“日志内容”显示信息
	private static final String CONTENT = "用户信息管理";
	@Autowired
	private IUserInfoService userInfoService;

	/**
	 * @Title: selectPage
	 * @Description: 分页查询
	 * @param userInfoDto
	 * @return
	 */
	@LogAnnotation(menuName = MENU_NAME, content = CONTENT, operateType = OperateTypeEnum.QUERY)
	@GetMapping
	@ApiOperation(value = "分页查询", httpMethod = "GET")
	@ApiImplicitParam(name = "userInfoDto", value = "查询参数", required = true)
	public Result selectPageVo(@Validated UserInfoDTO userInfoDto) {
		userInfoDto = (UserInfoDTO) InputInjectFilter.encodeInputString(userInfoDto,UserInfoDTO.class);
		Page<UserInfoDTO> page = new Page<>(userInfoDto.getPageNo(),userInfoDto.getPageSize());
		Page<UserInfoVO> result = userInfoService.selectPageVo(page, userInfoDto);
		return ResultGenerator.genOkResult(result);
	}	
	/**
	 * @Title: selectById
	 * @Description: 根据主键查询
	 * @param id
	 * @return
	 */
	@LogAnnotation(menuName = MENU_NAME, content = CONTENT, operateType = OperateTypeEnum.QUERY)
	@GetMapping("/{id}")
	@ApiOperation(value = "根据主键查询", httpMethod = "GET")
	@ApiImplicitParam(name = "id", value = "主键", required = true)
	public Result selectById(@PathVariable String id) {
		UserInfoVO userInfoVo = userInfoService.selectById(id);
		return ResultGenerator.genOkResult(userInfoVo);
	}
	/**
	 * @Title: deleteBatchIds
	 * @Description: 批量删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/{ids}")
	@LogAnnotation(menuName = MENU_NAME, content = CONTENT, operateType = OperateTypeEnum.DELETE)
	@ApiOperation(value = "批量删除", httpMethod = "GET")
	@ApiImplicitParam(name = "ids", value = "主键数组", required = true)
	public Result deleteBatchIds(@PathVariable("ids") String[] ids) {
		List<String> list = Arrays.asList(ids);
		int i = userInfoService.deleteBatchIds(list);
		return ResultGenerator.genOkResult(i);
	}
	/**
	 * @Title: insert
	 * @Description: 新增记录
	 * @param userInfoVo
	 * @return
	 */
	@PostMapping
	@LogAnnotation(menuName = MENU_NAME, content = CONTENT, operateType = OperateTypeEnum.INSERT)
	@ApiOperation(value = "新增记录", httpMethod = "POST")
	@ApiImplicitParam(name = "userInfoVo", value = "vo对象", required = true)
	public Result insert(@RequestBody @Validated UserInfoVO userInfoVo) {
		userInfoVo = (UserInfoVO) InputInjectFilter.encodeInputString(userInfoVo,UserInfoVO.class);
		userInfoVo.setOperatorId(this.getOperatorId());
		int i = 0;
		try {
			i = userInfoService.insert(userInfoVo);
		} catch (DuplicateUserNameException e) {
			e.printStackTrace();
			return ResultGenerator.genUnauthorizedResult(e.getMessage());
		}
		return ResultGenerator.genOkResult(i);
	}
	/**
	 * @Title: update
	 * @Description: 修改记录
	 * @param userInfoVo
	 * @return
	 */
	@PutMapping
	@LogAnnotation(menuName = MENU_NAME, content = CONTENT, operateType = OperateTypeEnum.UPDATE)
	@ApiOperation(value = "修改记录", httpMethod = "PUT")
	@ApiImplicitParam(name = "userInfoVo", value = "vo对象", required = true)
	public Result update(@RequestBody @Validated UserInfoVO userInfoVo) {
		userInfoVo = (UserInfoVO) InputInjectFilter.encodeInputString(userInfoVo,UserInfoVO.class);
		int i = userInfoService.update(userInfoVo);
		return ResultGenerator.genOkResult(i);
	}

}
